<!-- 拌合站管理 -->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
        <workSiteLeft ref="workSiteLeft" :show="show" @offerDeptTree="offerDeptTree" @showLeft="showLeft" @queryVal="queryVal"/>
      </el-col>

      <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="display: flex;justify-content: space-between;">
          <div>
            <el-button style="margin: 3px 0 0 0;" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle @click="showLeft(show)"></el-button>
            <el-form-item label="拌合站" prop="name">
              <el-input v-model="queryParams.name" placeholder="拌合站" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>

            <el-form-item label="是否智慧化改造" prop="isSmart" class="query">
              <el-select v-model="queryParams.isSmart" placeholder="是否智慧化改造" clearable @keyup.enter.native="handleQuery" style="width: 100%;">
                <el-option v-for="dict in dict.type.sys_yes_noremake" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['xtcsgl:sysStation:query']">搜索</el-button>
              <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
            </el-form-item>
          </div>

          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['xtcsgl:sysStation:add']">新增</el-button>
          </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5"><el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['xtcsgl:sysStation:add']">新增</el-button></el-col>
          <el-col :span="1.5"><el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['xtcsgl:sysStation:edit']">修改</el-button></el-col>
          <el-col :span="1.5"><el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['xtcsgl:sysStation:remove']">删除</el-button></el-col>
          <el-col :span="1.5"><el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['xtcsgl:sysStation:export']">导出</el-button></el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <el-table border v-loading="loading" :data="sysStationList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="拌合站" align="center" prop="name" width="220" show-overflow-tooltip/>
          <el-table-column label="编号" align="center" prop="identifier"/>
          <el-table-column label="规格型号" align="center" prop="model"/>
          <el-table-column label="是否接入" align="center" prop="isConnect">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.isConnect == '1'">已接入</el-tag>
              <el-tag type="info" v-else>未接入</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="物联卡号" align="center" prop="cardNum" show-overflow-tooltip />
          <el-table-column label="机长" align="center" prop="captain"/>
          <el-table-column label="机长电话" align="center" prop="captainPhone"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['xtcsgl:sysStation:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['xtcsgl:sysStation:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
      </el-col>
    </el-row>

    <!-- 添加或修改拌合站对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row :gutter="20">
          <el-col :span="12">
          <!-- <el-form-item label="工地名称" prop="baseName">-->
          <!--   <el-select v-model="form.baseCode" clearable placeholder="工地名称" style="width: 100%;">-->
          <!--     <el-option v-for="item in constructionList" :key="item.code" :label="item.name" :value="item.code"></el-option>-->
          <!--   </el-select>-->
          <!-- </el-form-item>-->

            <el-form-item label="所属标段" prop="baseCode">
              <el-cascader style="width: 100%;"
                v-model="form.baseCode" :show-all-levels="false" @change="changeBaseCode"
                :options="deptOptions"
                :props="{ checkStrictly: true, value: 'code', multiple: false }"
                clearable>
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="拌合站类型" prop="type">
              <el-select v-model="form.type" placeholder="拌合站类型" clearable style="width: 100%;">
                <el-option v-for="dict in dict.type.sys_station_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="拌合站编号" prop="code"><el-input v-model="form.code" placeholder="拌合站编号" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="编号" prop="identifier"><el-input v-model="form.identifier" placeholder="编号" /></el-form-item></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="设备厂商" prop="manufacturer"><el-input v-model="form.manufacturer" placeholder="设备厂商" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="规格型号" prop="model"><el-input v-model="form.model" placeholder="规格型号" /></el-form-item></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="购买日期" prop="buyTime" >
              <el-date-picker clearable  style="width: 100%;" v-model="form.buyTime" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择购买日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投产时间" prop="produceTime">
              <el-date-picker clearable style="width: 100%;" v-model="form.produceTime" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择投产时间"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="机长" prop="captain"><el-input v-model="form.captain" placeholder="机长" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="机长电话" prop="captainPhone"><el-input v-model="form.captainPhone" placeholder="机长电话" /></el-form-item></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="厂商联系人" prop="service"><el-input v-model="form.service" placeholder="厂商联系人" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="厂商电话" prop="servicePhone"><el-input v-model="form.servicePhone" placeholder="厂商电话" /></el-form-item></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否智慧化改造" prop="isSmart">
              <el-select v-model="form.isSmart" placeholder="是否智慧化改造" clearable @keyup.enter.native="handleQuery" style="width: 100%;">
                <el-option v-for="dict in dict.type.sys_yes_noremake" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12"><el-form-item label="是否接入" prop="isConnect">
              <el-radio-group v-model="form.isConnect">
                <el-radio :label="1">已接入</el-radio>
                <el-radio :label="0">未接入</el-radio>
              </el-radio-group>
            </el-form-item></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="物联卡号" prop="cardNum"><el-input v-model="form.cardNum" placeholder="物联卡号" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="备注" prop="info"><el-input type="textarea" :rows="2" v-model="form.info" placeholder="备注" /></el-form-item></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysStation, getSysStation, delSysStation, addSysStation, updateSysStation } from "@/api/xtcsgl/sysStation";
import { deptListData } from "@/api/zhgd/hntManage";
import workSiteLeft from "@/views/components/workSiteLeft";

export default {
  name: "SysStation",
  dicts: ['sys_yes_no', 'sys_yes_noremake', 'sys_station_type'],
  data() {
    return {
      deptOptions: undefined,
      show: true,
      rowObj: {
        T1: 3,
        T2: 24,
        T3: 21,
        T4: 24,
        F1: 0,
        F2: 0,
        F3: 24,
        F4: 24,
      },
      radio: 0,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拌合站表格数据
      sysStationList: [],
      // 工地名称列表数据
      constructionList: [],
      // 拌合站搜索框数据
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        baseCode: undefined,
        type: undefined,
        name: undefined,
        code: undefined,
        identifier: undefined,
        captain: undefined,
        captainPhone: undefined,
        buyTime: undefined,
        produceTime: undefined,
        manufacturer: undefined,
        model: undefined,
        service: undefined,
        servicePhone: undefined,
        sensorConfig: undefined,
        materialConfig: undefined,
        info: undefined,
        creator: undefined,
        createDate: undefined,
        updater: undefined,
        updateDate: undefined,
        deleted: undefined,
        isSmart: undefined,
        isConnect:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        baseCode: [{ required: true, message: "所属标段不能为空", trigger: "blur" }],
        names: [{ required: true, message: "工地名称不能为空", trigger: "blur" }],
        type: [{required: true, message: "拌合站类型不能为空", trigger: "blur"}],
        isConnect: [{ required: true, message: '请确认设备是否接入', trigger: 'change' }],
      
      }
    };
  },
  components: {
    workSiteLeft,
  },
  created() {
    this.getList();
    this.getProjectList();
  },
  methods: {
    changeBaseCode (val) {
      if (val != undefined) {
        if (val.length > 0) {
          this.form.baseCode = val[val.length - 1];
        } else {
          this.form.baseCode = null;
        }
      }
    },
    offerDeptTree(deptOptions) {
      this.deptOptions = deptOptions;
    },
    // 左侧树点击事件
    showLeft(show) {
      this.show = !show;
    },
    queryVal(val) {
      this.queryParams.id = val.id;
      this.handleQuery();
    },
    /** 查询拌合站列表 */
    getList() {
      this.loading = true;
      listSysStation(this.queryParams).then(response => {
        this.sysStationList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询拌合站列表 */
    getProjectList() {
      deptListData(this.queryParams).then((response) => {
        this.stationList = response;
      });
    },
    changeProjectCatalog(val) {
      if (val != undefined ) {
        if (val.length > 0) {
          this.form.projectCatalog = val[val.length - 1];
        } else {
          this.form.projectCatalog = null;
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        baseCode: undefined,
        type: undefined,
        name: undefined,
        code: undefined,
        identifier: undefined,
        captain: undefined,
        captainPhone: undefined,
        buyTime: undefined,
        produceTime: undefined,
        manufacturer: undefined,
        model: undefined,
        service: undefined,
        servicePhone: undefined,
        sensorConfig: undefined,
        materialConfig: undefined,
        info: undefined,
        creator: undefined,
        createDate: undefined,
        updater: undefined,
        updateDate: undefined,
        deleted: undefined,
        isSmart: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.workSiteLeft.resetQuery();
      this.queryParams.id = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拌合站";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSysStation(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改拌合站";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSysStation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSysStation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(row.id ? `是否确认删除编号为"${row.identifier}"的数据项？` : '是否确认删除选中数据项？').then(() => {
        this.loading = true;
        return delSysStation(ids);
      })
      .then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhgd/sysStation/export', {
        ...this.queryParams
      }, `sysStation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .query {
  .el-form-item__label {
    width: 110px !important;
  }
}
</style>
